package com.nfc.lock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nfc.lock.common.result.Result;
import com.nfc.lock.entity.WarningRecord;
import com.nfc.lock.service.IWarningRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预警记录Controller
 */
@Tag(name = "预警管理")
@RestController
@RequestMapping("/api/v1/warning")
@RequiredArgsConstructor
public class WarningRecordController {

    private final IWarningRecordService warningRecordService;

    @Operation(summary = "分页查询预警记录")
    @GetMapping("/list")
    public Result<IPage<WarningRecord>> getWarningList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "预警类型") @RequestParam(required = false) Integer warningType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "锁具ID") @RequestParam(required = false) Long lockId
    ) {
        return Result.success(warningRecordService.getWarningList(pageNum, pageSize, warningType, status, lockId));
    }

    @Operation(summary = "获取预警详情")
    @GetMapping("/{warningId}")
    public Result<WarningRecord> getWarningDetail(
            @Parameter(description = "预警ID") @PathVariable Long warningId
    ) {
        return Result.success(warningRecordService.getWarningDetail(warningId));
    }

    @Operation(summary = "处理预警")
    @PostMapping("/handle/{warningId}")
    public Result<Void> handleWarning(
            @Parameter(description = "预警ID") @PathVariable Long warningId,
            @Parameter(description = "处理说明") @RequestParam(required = false) String handleRemark
    ) {
        warningRecordService.handleWarning(warningId, handleRemark);
        return Result.success();
    }

    @Operation(summary = "创建预警记录")
    @PostMapping("/create")
    public Result<Void> createWarning(@RequestBody WarningRecord warningRecord) {
        warningRecordService.createWarning(warningRecord);
        return Result.success();
    }
}
